package ua.eug.robov2.schematic;

import ua.eug.robov2.ImageProvider;
import ua.eug.robov2.utils.Vector2i;

import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class SheetBorder {
	private final static TextureRegion border = ImageProvider.FindRegion("schborder");
	private final static int borderLen = 4; // length in map squares
	private final static TextureRegion corner = ImageProvider.FindRegion("schcorner");
	private float scale;
	private Vector2i size;
	private Vector2i brd;

	public SheetBorder (Vector2i border, Vector2i size) {
		this.brd = border;
		this.size = size;
		scale = 8;
		System.out.println("SheetBorder (" + brd.x + ", " + brd.y + ")  Size : " + size);
	}
	
	public void draw(Batch batch, Vector2i origin) {
		int i, shift;
		// TODO
		
		if (origin.x == -1) { // left border
			if (origin.y == -1) // bottom corner
				batch.draw(corner,0,1*scale,0,0,scale,scale,1,1,270);
			if (origin.y >= brd.y) // top corner
				batch.draw(corner, 1*scale, (size.y-origin.y+1)*scale ,0,0,scale,scale,1,1,180);
			// left line
			shift = (origin.y+1)%borderLen-1;
			for (i=borderLen; i<(size.y-origin.y+borderLen-1); i+=borderLen) {
				batch.draw(border,0*scale,(i-shift)*scale,0,0,borderLen*scale, scale, 1,1, 270);
			}
		}
		
		if (origin.x >= brd.x ) { // right border
			if (origin.y == -1) // bottom corner
				batch.draw(corner,(size.x-origin.x)*scale,0,0,0,scale,scale,1,1,0);
			if (origin.y >= brd.y) // top corner
				batch.draw(corner, (size.x-origin.x+1)*scale, (size.y-origin.y)*scale,0,0,scale,scale,1,1,90);	
			// right
			shift = (origin.y)%borderLen;
			for (i=0; i<=(size.y-origin.y-1); i+=borderLen) {
				batch.draw(border,(size.x-origin.x+1)*scale,(i-shift)*scale,0,0,borderLen*scale, scale, 1,1, 90);
			}
		}
		
		if (origin.y == -1) { // bottom border
			shift = origin.x%borderLen;
			for (i=0; i<(size.x-origin.x); i+=borderLen) {
				batch.draw(border,(i-shift)*scale,0*scale,0,0,borderLen*scale, scale, 1,1, 0);
			}
		}
		if (origin.y >= brd.y) { // top border
			shift = (origin.x)%borderLen;
			for (i=borderLen; i<(size.x-origin.x+borderLen); i+=borderLen) {
				batch.draw(border,(i-shift)*scale,(size.y-origin.y+1)*scale,0,0,borderLen*scale, scale, 1,1, 180);
			}
		}
	}
	
	public void resize(float s) {
		scale = s;
	}
}
